import { defineStore } from 'pinia';
import { getRightData } from '@/api/earlyWarning';
import { useCesiumPoints } from '@/hooks/useCesiumPoints';
import { useDialogStore } from '@/store/modules/useDialogStore';

export interface TableRow {
  occurrenceTime: string;
  typeName: string;
  warningLevel: string;
  occurrenceAddress: string;
  statusName: string;

  [key: string]: any;
}

export const useIncidentDispositionStore = defineStore('incidentDisposition', () => {
  const tableData = ref<TableRow[]>([]);
  const { addPoint, clearAllPoints } = useCesiumPoints();
  const dataSource = reactive<Record<string, any[]>>({});
  const { flyTo } = useDialogStore();

  const goRiskDetection = (row, name) => {
    flyTo({ longitude: 117.140035075, latitude: 36.798747059 }, 400000);
    if (dataSource[name]) {
      clearAllPoints(dataSource[name]);
      delete dataSource[name];
    } else {
      dataSource[name] = [addPoint(row, name)];
    }
  };
  const initClearPoints = () => {
    Object.keys(dataSource).forEach((key) => {
      clearAllPoints(dataSource[key]);
      delete dataSource[key];
    });
  };
  
  // 初始化警告信息数据
  const initWarningInfos = async () => {
    const { data } = await getRightData({ type: 'waringInfos' });
    tableData.value = data.waringInfos;
  };
  
  return {
    tableData,
    goRiskDetection,
    initClearPoints,
    initWarningInfos
  };
});
